
/* Generated by Interface Builder */

#import "IFolderCell.h"
#import "TransparentWindow.h"
#import "IDocTransparentWindow.h"
#import "IReceiveSpeaker.h"
#import "IReceiveListener.h"
#import "IFolderMatrix.h"
#import "errdebug.h"
#import "Localization.h"
#import "data_types.h"
#import <appkit/NXImage.h>
#import <appkit/Panel.h>
#import <dpsclient/dpsNeXT.h>
#import <string.h>
#import <dpsclient/wraps.h>
#import <appkit/graphics.h>

#define LoStr(key)      doLocalString(NULL,key,NULL)

@implementation IFolderCell

- initIconCell:(const char *)iconName
{
    [super initIconCell:iconName];
    [self setTarget:self];
    [self setAction:@selector(selectCellInBrowser:)];
    
    iAltImage = NULL;
    return self;
}

- setLeaf:(BOOL)flag
{
    iLeafFlag = flag;
    return self;
}

- (BOOL)isLeaf
{
    return (iLeafFlag);
}

- drawInside:(const NXRect *)aRect inView:controlView
{
    id		triangle;
    NXPoint	triPoint;
    NXRect	iconFrame;
    NXPoint	iconOrigin;
    
    // do normal stuff
    [super drawInside:aRect inView:controlView];
    
    // draw triangle for directory cell
    if (iLeafFlag == NO) {
	triPoint.x = aRect->size.width - 16.0;
	triPoint.y = (aRect->size.height) / 2;
	triangle = [NXImage findImageNamed:"triangle"];
	[triangle composite:NX_SOVER toPoint:&triPoint];
    }

    // draw alternative image (open folder)
    if (iAltImage != NULL) {
	iconFrame = *aRect;
	[self getIconRect:&iconFrame];
	iconOrigin = iconFrame.origin;
	if ([controlView isFlipped] == YES) {
	    // control view is Matrix, so usually control view's coordinate
	    // is flipped. 
	    // reft down corner should be origin for composite view, but
	    // reft up corner is {0.0, 0.0} when flipped.
	    iconOrigin.y = iconFrame.origin.y + iconFrame.size.height;
	}
	[iAltImage composite:NX_SOVER toPoint:&(iconOrigin)];
    }

    return self;
}

/* When entering the window, show the Icon being dragged as a ghost in the */
/* well */
- (BOOL)windowEntered:dragSource controlView:cView
{
    id		image;
    NXAtom	closedFolderAtom = NXUniqueString(CLOSEDFOLDER);
    NXAtom	closedMultiFolderAtom = NXUniqueString(CLOSEDMULTIFOLDER);
    NXAtom	nextStationAtom = NXUniqueString(NEXTSTATION);
    NXAtom	selfImageAtom = NXUniqueString([[self image] name]);
    
    if ((image =[dragSource image]) == [NXImage findImageNamed:MIMEDOCICON]) {
	if (selfImageAtom == closedFolderAtom) {
	    // dragged window is mime document and this cell has 
	    // newsgroup folder
	    iAltImage = [NXImage findImageNamed:OPENFOLDER];
	} else if (selfImageAtom == closedMultiFolderAtom) {
	    // cell has multi folder
	    iAltImage = [NXImage findImageNamed:OPENMULTIFOLDER];
	} else if (selfImageAtom == nextStationAtom) {
	    // cell has next station icon
	    iAltImage = [NXImage findImageNamed:NEXTALTSTATION];
	} else {
	    // default
	    return NO;
	}
	[[cView window] disableFlushWindow];
	[cView drawCell:self];
	[[cView window] reenableFlushWindow];
	return YES;
    } else {
	return NO;
    }
}

/* When exiting the window, remove the ghost icon from the well */
- (BOOL)windowExited:dragSource controlView:cView
{
    id		image;
    NXAtom	closedFolderAtom = NXUniqueString(CLOSEDFOLDER);
    NXAtom	closedMultiFolderAtom = NXUniqueString(CLOSEDMULTIFOLDER);
    NXAtom	nextStationAtom = NXUniqueString(NEXTSTATION);
    NXAtom	selfImageAtom = NXUniqueString([[self image] name]);
    
    if (((image =[dragSource image]) == [NXImage findImageNamed:MIMEDOCICON]) 
    	&& ((selfImageAtom == closedFolderAtom) 
			|| (selfImageAtom == closedMultiFolderAtom)
			|| (selfImageAtom == nextStationAtom))) { 
	// dragged out window is mime document 
	// and this cell has newsgroup folder
	iAltImage = NULL;
	[[cView window] disableFlushWindow];
	[cView drawCell:self];
	[[cView window] reenableFlushWindow];
	return YES;
    } else {
	return NO;
    }
}

/* When dropping the Icon in the well, draw it there and message the */
/* delegate so that it can modify the Inspector view accordingly */
- (BOOL)windowDropped:dragSource controlView:cView
{
    id		image;
    port_t      port;
    int         returnCode;
    char	newsgroupName[512];
    NXAtom	closedFolderAtom = NXUniqueString(CLOSEDFOLDER);
    NXAtom	closedMultiFolderAtom = NXUniqueString(CLOSEDMULTIFOLDER);
    NXAtom	nextStationAtom = NXUniqueString(NEXTSTATION);
    NXAtom	selfImageAtom = NXUniqueString([[self image] name]);
    
    if ((image =[dragSource image]) == [NXImage findImageNamed:MIMEDOCICON]) {
	if (selfImageAtom == closedFolderAtom 
		|| selfImageAtom == closedMultiFolderAtom 
		|| selfImageAtom == nextStationAtom) {
	    // dropped window is mime document and this cell has 
	    // newsgroup folder or nextStation icon

	    iAltImage = NULL;	// close folder
	    [cView drawCell:self];
	    // (char *)title represents newsgroup names
	    // if folder is multiple selected, newsgorup name will be 
	    // splited by ", ".
	    if (selfImageAtom == nextStationAtom) {
		strncpy(newsgroupName, "", 1);
	    } else {
		strncpy(newsgroupName, [self title], sizeof(newsgroupName) -1);
	    }
	} else {
	    return NO;
	}
	
	if ((port = NXPortFromName(MMEDITOR, NULL)) == PORT_NULL) {
	    NXRunAlertPanel([self name],LoStr("%s is an unknown port."),
		LoStr("OK"),NULL,NULL, MMEDITOR);
	}
	[[NXApp appSpeaker] setSendPort:port];
	
	if ((returnCode = [[NXApp appSpeaker] 
		requestForPost:(char *)newsgroupName
		sourceWindow:(int)[dragSource sourceWindowNum]]) != 0) {
	    NXRunAlertPanel([self name],LoStr("cannot contact %s."),
                LoStr("OK"), NULL, NULL, MMEDITOR);
	}
	return YES;
    } else {
	return NO;
    }
}

@end
